// 保存图片到我的空间
// 获取spaceId
import { ref } from 'vue'
import { listSpaceVoByPageUsingPost } from '@/api/spaceController.ts'
import { SPACE_TYPE_ENUM } from '@/constants/space.ts'
import { useUserStore } from '@/stores/user.ts'
import { uploadPictureByUrlUsingPost } from '@/api/pictureController.ts'
import { useRouter } from 'vue-router'


const router = useRouter()
const userStore = useUserStore()
const space = ref<API.SpaceVO>({})
const checkAuth = async () => {
  // 获取用户空间
  const res = await listSpaceVoByPageUsingPost({
    spaceType: SPACE_TYPE_ENUM.PRIVATE,
    userId: userStore?.userInfo?.id,
  })
  if (res.data.code === 0 && res.data.data) {
    // @ts-ignore
    space.value = res.data.data?.records?.[0]
  }
}

const loading = ref(false)
// 上传图片url
const handleUpload = async (url: string, name: string) => {
  loading.value = true
  const res = await uploadPictureByUrlUsingPost({
    spaceId: space.value.id,
    fileUrl: url,
    picName: name,
  })
  if (res.data.code != 0) {
    return
  }
  loading.value = false
}

// 加入我的空间
export const doAddPicture = async (url: string, name: string) => {
  await checkAuth()
  console.log(space.value)
  await handleUpload(url, name)
}
